package GUI; //done

import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client.ConnectionToServer;

/**
 * This Class controls Address Zone Window. 
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class AddressZoneWindow extends JPanel{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The address Zone ID TextField . */
	private JTextField addressZoneIDTextField = null;
	
	/** The address Zone Name TextField . */
	private JTextField AddressZoneNameTextField = null;
	
	/** The all Address Zones Label. */
	private JLabel allAddressZoneLabel= null;
	
	/** The address Zone ID Label. */
	private JLabel addressZoneIDLabel = null;
	
	/** The address Zone Name Label. */
	private JLabel addressZoneNameLabel = null;
	
	/** The add Button. */
	private JButton addButton = null;
	
	/** The update Button. */
	private JButton updateButton = null;
	
	/** The cancel Button. */
	private JButton cancelButton = null;
	
	/** The refresh Button. */
	private JButton refreshButton = null;
	
	/** The all Address Zone JComboBox. */
	private JComboBox allAddressZoneJComboBox= null;
	
	/** The Background Label. */
	private JLabel BackgroundLabel;
	
	/**
	 * Default constructor
	 */
	public AddressZoneWindow() {
		super();
		setBackground(SystemColor.activeCaption);
		initialize();	
	}
	
	/**
	 * This function initializes AddressZoneWindow
	 */
	private void initialize() {
		allAddressZoneLabel = new JLabel();
		allAddressZoneLabel.setBounds(new Rectangle(15, 20, 130, 22));
		allAddressZoneLabel.setText("All address zones:");
		
		addressZoneIDLabel = new JLabel();
		addressZoneIDLabel.setBounds(new Rectangle(15, 53, 130, 22));
		addressZoneIDLabel.setText("Address zone ID:");
		
		addressZoneNameLabel = new JLabel();
		addressZoneNameLabel.setBounds(new Rectangle(15, 86, 130, 22));
		addressZoneNameLabel.setText("Address zone Name:");
		
		this.setSize(294, 179);
		this.setLayout(null);
		this.add(allAddressZoneLabel, null);
		this.add(addressZoneIDLabel, null);
		this.add(addressZoneNameLabel, null);
		this.add(getAddressZoneIDTextField(), null);
		this.add(getAddressZoneNameTextField(), null);
		this.add(getAllAddressZonesJComboBox(), null);
		this.add(getRefreshButton(),null);				
	}
	
	/**
	 * This function initializes addressZoneIDTextField	
	 */
	private JTextField getAddressZoneIDTextField(){
		if (addressZoneIDTextField == null) {
			addressZoneIDTextField = new JTextField();
			addressZoneIDTextField.setBounds(new Rectangle(150, 52, 127, 25));
		}
		return addressZoneIDTextField;
	}
	
	/**
	 * This function initializes AddressZoneNameTextField	
	 */
	private JTextField getAddressZoneNameTextField(){
		if (AddressZoneNameTextField == null) {
			AddressZoneNameTextField = new JTextField();
			AddressZoneNameTextField.setBounds(new Rectangle(150, 85, 127, 25));
		}
		return AddressZoneNameTextField;
	}
	
		
	/**
	 * This function initializes allAreaJComboBox	
	 */
	private JComboBox getAllAddressZonesJComboBox(){
		if (allAddressZoneJComboBox == null) {
			allAddressZoneJComboBox = new JComboBox();
			allAddressZoneJComboBox.setBounds(new Rectangle(150, 20, 127, 25));
			updateallAreaJComboBox();			
		}
		return allAddressZoneJComboBox;
	}
	
	/**
	 * This function update allAreaJComboBox 
	 */
	private void updateallAreaJComboBox(){
		try{
			int i=0,itemcount = allAddressZoneJComboBox.getItemCount();
			if (itemcount > 0) {
				allAddressZoneJComboBox.addItem(":");
				allAddressZoneJComboBox.setSelectedItem(":");				
			}
			for (i=0; i < itemcount ; i++ ){				
				allAddressZoneJComboBox.removeItemAt(0);						
			}					
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}			
		String[] areaList = null;
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			areaList = (String[]) connectiontoserver.loadAllAddressZones();
			if (areaList == null){
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				allAddressZoneJComboBox.addItem(" ");
			}	
			else{
				int len = areaList.length;
				if (len == 0)
					allAddressZoneJComboBox.addItem("0:EMPTY");	
				else
					allAddressZoneJComboBox.addItem(" ");
				for(int i=0; i < len; i++){
					allAddressZoneJComboBox.addItem(areaList[i]);
				}	
			}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "Error!");
			allAddressZoneJComboBox.addItem(" ");
		}			
	}
	
	/**
	 * This function update TextFields to area details
	 */
	private void  updateTextField(){
		String area = (String)allAddressZoneJComboBox.getSelectedItem();
		addressZoneIDTextField.setText(area.substring(0, area.indexOf(":")));	
		AddressZoneNameTextField.setText(area.substring(area.indexOf(":")+1, area.length()));			
	}
	
	/**
	 * This function initializes cancelButton
	 */	
	public JButton getCancelButton(){
		if (cancelButton == null){
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setBounds(new Rectangle(15, 130, 80, 29));
			cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				}				
			});						
		}		
		return cancelButton;				
	}
	
	/**
	 * This function initializes SaveButton	
	 */
	private JButton getSaveButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setBounds(new Rectangle(195, 130, 80, 29));
			addButton.setText("ADD");
			addButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					saveArea();
				}					
			});						
		}
		return addButton;
	}	
	
	/**
	 * This function update TextFields to area details
	 */
	private void  saveArea(){		
		Object msgfromsetrver = null;
		try{
			int areaID = Integer.parseInt(addressZoneIDTextField.getText());	
			String areaName = AddressZoneNameTextField.getText();
			ConnectionToServer connectiontoserver = new ConnectionToServer();
			msgfromsetrver = (String)connectiontoserver.addAddressZone(areaID, areaName);
			if (msgfromsetrver != null){
				if (msgfromsetrver.equals("OK"))
					JOptionPane.showMessageDialog(null, "The Adress Zone saved successfully");
				else
					JOptionPane.showMessageDialog(null, "Address Zone ID already exists !","WARNING", JOptionPane.WARNING_MESSAGE);
			}		
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);		
		}					
	}
	
	/**
	 * This function initializes updateButton	
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton();
			updateButton.setBounds(new Rectangle(195, 130, 80, 29));
			updateButton.setText("Update");
			updateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					updateArea();
				}					
			});						
		}
		return updateButton;
	}
	
	/**
	 * This function update area details in data base
	 */
	private void  updateArea(){		
		int areaID = Integer.parseInt(addressZoneIDTextField.getText());	
		String areaName = AddressZoneNameTextField.getText();
		Object msgfromsetrver = null;
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();
			msgfromsetrver = (String)connectiontoserver.updateAddressZone(areaID, areaName);
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);		
		}
		if (msgfromsetrver != null){
			if (msgfromsetrver.equals("OK"))
				JOptionPane.showMessageDialog(null, "The area successfully update");
			else
				JOptionPane.showMessageDialog(null, "The area not update!!!","WARNING", JOptionPane.WARNING_MESSAGE);
		}		
	}
		
	/**
	 * This function initializes refreshButton	
	 */
	private JButton getRefreshButton() {
		if (refreshButton == null) {
			refreshButton = new JButton();
			refreshButton.setBounds(new Rectangle(105, 130, 80, 29));
			refreshButton.setText("Refresh");
			refreshButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					AddressZoneNameTextField.setText("");
					addressZoneIDTextField.setText("");
					updateallAreaJComboBox();
				}					
			});						
		}
		return refreshButton;
	}
	
	/**
	 * This function add UpdateButton to the FORM
	 */
	public void addUpdateButton(){
		this.add(getUpdateButton(),null);
		addressZoneIDTextField.setEditable(false);
		allAddressZoneJComboBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				updateTextField();
			}	
		});			
	}
	
	/**
	 * This function add SaveButton to the FORM
	 */
	public void addSaveButton(){
		this.add(getSaveButton(),null);
	}
}
